package com.ceba.test.modules.admin.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ceba.cebautils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.base.configs.CebaPathUtil;
import com.ceba.test.base.language.ResourceUtils;
import com.ceba.base.web.service.IBaseService;

import abc.ID;
import abc.Result;
import abc.util.FileHelper;
import abc.util.ZipUtils;
import abc.web.HttpContext;

import javax.servlet.http.HttpServletRequest;

@SuppressWarnings("deprecation")
public class LogService extends IBaseService{
	static Logger logger = LoggerFactory.getLogger(LogService.class);

	
	
	@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
	public void toToDownLoad(String homeUr, long d, Result result, HttpServletRequest request, String sign_Timer_Log) throws Exception{
		String luri = homeUr.substring(0, homeUr.lastIndexOf("upfile"));
		String uri = luri + "logs\\";;
		String nowDate = DateUtil.getNowTime(DateUtil.YYYY_MM_DD_);
		Map<String,String> downFiles = new<String,String> HashMap();
		Collection<File> files = FileHelper.listFiles(uri);
		
		String suri = luri + "s_logs\\";
		Collection<File> sfiles = FileHelper.listFiles(suri);
		
		String contextPath ="";
		if(request !=null){
			contextPath = request.getRequestURL().toString();
			contextPath = contextPath.replace("action", "");
		}
		if(files !=null && !files.isEmpty()){
			Iterator it = files.iterator();
			while(it.hasNext()){
				File f = (File) it.next();
				String name = f.getName();
				long lastModified = f.lastModified();
				String fd = DateUtil.parseDateFromLongTime(lastModified, DateUtil.YYYY_MM_DD_);
				if(name.contains("log.cb") && DateUtil.getRangeTime2(fd, nowDate,DateUtil.YYYY_MM_DD_)<= d){
					downFiles.put(name, f.getPath());
					//System.out.println(f.getPath() + " " + fd);
				}
			}
			
			//签名记录
			Iterator its = sfiles.iterator();
			while(its.hasNext()){
				File f = (File) its.next();
				String name = f.getName();
				downFiles.put(name, f.getPath());
			}
			
			//添加凌晨统计签名份数
			if(new File(sign_Timer_Log).exists()){
				downFiles.put("sign.cb", sign_Timer_Log);
			}
			String id = ID.getGuid();
			//创建下载的目录
			String downFileDir = homeUr+"temp\\logs\\"+nowDate+"\\"+id;
			if(downFiles.size() >0){
				List<String> list = new ArrayList(downFiles.keySet());
				for(int i = 0 ; i < list.size(); i++){
					String fileName = list.get(i);
					String filePath = downFiles.get(fileName);
					boolean flag = FileHelper.copyFile(filePath, downFileDir+"\\"+fileName);
				}
				ZipUtils.zip(downFileDir, downFileDir+".zip");
				
				result.setData(contextPath+"upfile\\temp\\logs\\"+nowDate+"\\"+id+".zip");
			}else{
				result.setResult(false);
				result.set("error",ResourceUtils.getMessage("MODULES_NO_FILE"));
			}
			
		}
	}
	
	
	
	
	public static void main(String[]args){
		String homeUr = "E:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\ep\\upfile\\";
		long d = 1*24*60*60*1000; 
		LogService locService = new LogService();
		try {
			locService.toToDownLoad(homeUr, d, new Result(true),null,null);
		} catch (Exception e) {
			
		}
	}

}
